//
//  TravelViewController.m
//  HZHome
//
//  Created by Alawn on 13-10-8.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "TravelViewController.h"
#import "UIImageView+WebCache.h"
#import "WetLandBookingViewController.h"
#import "BookingListViewController.h"
#import "UserInfoService.h"
#import "RegisterViewController.h"
#import <UIKit/UIKit.h>
#import "WetlandService.h"
#import "WetLandBookingOrderViewController.h"
#import "BookingNoticeViewController.h"
@interface TravelViewController ()

@property (strong, nonatomic) NSArray *ticketsList;
@property (strong, nonatomic) NSMutableArray *checkedList;

@end

@implementation TravelViewController
@synthesize dictRoute;
int* showNotice = 0;//如果==1 显视用户须知  ，预定时判断
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background1.png"]]];
//    
//    NSString * labelStr =@" 海珠湿地位于广州市中心城区，海珠区东南部，被称为广州市的“南肺”和“肾”，主要包括万亩果园、海珠湖及相关河涌39条，总用地面积约800公顷，水域面积达380公顷，是珠三角河涌湿地、城市内湖湿地与半自然果林镶嵌交混的复合湿地生态系统海珠湿地积淀了珠江三角洲湿地千年的果基农业文化，富有岭南水乡文化特色，是岭南文化重要的发源地。";
//    [self.HzsdContent setText:labelStr];
//    CGSize labelSize = {0, 0};
//    labelSize = [labelStr sizeWithFont:[UIFont systemFontOfSize:17]
//                     constrainedToSize:CGSizeMake(200.0, 5000)
//                         lineBreakMode:UILineBreakModeWordWrap];
//    //14 为UILabel的字体大小
//    //200为UILabel的宽度，5000是预设的一个高度，表示在这个范围内
//    self.HzsdContent.numberOfLines = 0;//表示label可以多行显示
//    self.HzsdContent.lineBreakMode = UILineBreakModeCharacterWrap;//换行模式，与上面的计算保持一致。
//    self.HzsdContent.frame = CGRectMake(self.HzsdContent.frame.origin.x, self.HzsdContent.frame.origin.y, self.HzsdContent.frame.size.width, labelSize.height);//保持原来Label的位置和宽度，只是改变高度。
    self.scrollView.hidden=YES;
    self.bookingButton.hidden=YES;
    if (dictRoute!=nil&&dictRoute.count>0) {
        self.btnPhone.hidden=YES;
        self.imgPhone.hidden=YES;
        self.LableBuss1.hidden=YES;
        self.HzsdLableBottom.hidden = YES;
        UIImage *img = [UIImage imageNamed:[dictRoute objectForKey:@"image"]];
        self.headImgView.image=img;
        self.HzsdLable.text=[dictRoute objectForKey:@"item1"];
        self.HzsdContent.text=[dictRoute objectForKey:@"item2"];
        self.labelAddress.text=[dictRoute objectForKey:@"item3"];
        self.LableBuss2.text=[dictRoute objectForKey:@"item4"];
    } else if (_dataItem) {
        //从海珠十景进入时的页面初始化
        __block UIActivityIndicatorView *activityIndicator;
        __weak UIImageView *weakImageView = self.headImgView;
        [self.headImgView setImageWithURL:[NSURL URLWithString:_dataItem.picture] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize)
        {
            if (!activityIndicator)
            {
                [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                activityIndicator.center = weakImageView.center;
                [activityIndicator startAnimating];
            }
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
        {
            [activityIndicator removeFromSuperview];
            activityIndicator = nil;
        }];
        self.title = _dataItem.title;
        self.HzsdLable.text = _dataItem.title;
        self.HzsdContent.text = _dataItem.detail;
        [self.btnPhone setTitle:_dataItem.phone forState:UIControlStateNormal];
        self.labelAddress.text = _dataItem.address;
        self.LableBuss1.text = _dataItem.station;
        self.LableBuss2.text = _dataItem.transit;
        self.imgPhone.hidden=YES;
        self.imgAddress.hidden=YES;
        self.imgBus.hidden=YES;
        self.btnPhone.hidden=YES;
        self.labelAddress.hidden=YES;
        self.LableBuss2.hidden=YES;
        self.LableBuss1.hidden=YES;
    }
    [self.scrollView addSubview:self.contentView];
    self.scrollView.contentSize = CGSizeMake(320.0f, 666.0f);
    if (self.config!=nil) {
        NSString *myValue=[self.config objectForKey:@"key"];
        self.title=myValue;
    }
    if ([self.isLuxian isEqualToString:@"路线推荐"]) {
        self.bookingButton.hidden =YES;

        CGRect frame = self.scrollView.frame;
        frame.size.height += CGRectGetHeight(self.bookingButton.frame);
        self.scrollView.frame = frame;
        //调整高度
        CGFloat infoHeight = [self caculateHeightForString:self.HzsdContent.text AndFontSize:14.0 AndWitdh:300];
        CGRect content = self.HzsdContent.frame;
        content.size.height = infoHeight;
        self.HzsdContent.frame = content;
        self.scrollView.contentSize = CGSizeMake(320.0f, 666.0f);
  
        self.isLuxian = @"";
        self.scrollView.hidden=NO;//显视
        NSLog(@"路线推荐----");

    }
    else if ([self.title hasPrefix:@"海珠湿地"] || [self.title hasPrefix:@"湿地公园"]) {
        
        //读取票面信息
        [self startLoadingViewWithText:@"正在加载"];
        [[WetlandService new] getTicketsList:^(NSArray *data) {
            self.ticketsList = data;
            self.checkedList = [NSMutableArray arrayWithCapacity:data.count];
            for (int i = 0, count = data.count; i < count ; i ++) {
                [self.checkedList addObject:[NSNumber numberWithBool:NO]];
                
            }
            //[self.tableView reloadData];
            [self stopLoadingView];
            self.scrollView.hidden=NO;//显视
            self.bookingButton.hidden=NO;
        } Failure:^(NSString *msg) {
            [self showAlertWithTitle:@"错误" Msg:msg];
            [self stopLoadingView];
        }];
        self.navigationItem.rightBarButtonItem = rightNavBtnWithImgName(@"预约记录", self, @selector(rightBtAction:));
        
        
        
        
    } else {
        self.scrollView.hidden=NO;
        self.bookingButton.hidden =YES;
        CGRect frame = self.scrollView.frame;
        frame.size.height += CGRectGetHeight(self.bookingButton.frame);
        self.scrollView.frame = frame;
        //调整高度
        CGFloat infoHeight = [self caculateHeightForString:self.HzsdContent.text AndFontSize:14.0 AndWitdh:300];
        CGRect content = self.HzsdContent.frame;
        content.size.height = infoHeight;
        self.HzsdContent.frame = content;
        self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(content));
    }
    
    self.HzsdLable.textAlignment=NSTextAlignmentCenter;
    [self.scrollView setBackgroundColor:[UIColor clearColor]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.HzsdLableBottom.hidden = YES;

}
#pragma mark - Button Methods
-(void)rightBtAction:(id)sender
{
    if (![UserInfoService getUserInfoService].currentLoginUserName) {
        UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该功能注册用户才能使用。现在注册" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        [alertview show];
        return;
    }
    BookingListViewController *listVC = [[BookingListViewController alloc] initWithNibName:@"BookingListViewController" bundle:nil];
    [self.navigationController pushViewController:listVC animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)callPhone:(id)sender
{
    NSString* numberAfterClear =self.btnPhone.titleLabel.text;
    NSURL *phoneNumberURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", numberAfterClear]];
    NSLog(@"make call, URL=%@", phoneNumberURL);
    [[UIApplication sharedApplication] openURL:phoneNumberURL];
}
//预定事件
- (IBAction)bookingButtonAction:(id)sender
{
    if (![UserInfoService getUserInfoService].currentLoginUserName) {
        UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该功能注册用户才能使用。现在注册" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        [alertview show];
        return;
    }
    NSLog(@"点击了预定");
    
//    showNotice = 1;
//    UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"预定须知"
//     message:@"1.每日可预约3天之内的门票(不含当日)\r\n2.每人每日最多可预约免费门票3张,参观当日凭预约号和预约时使用的手机号码到公园正门票务处领票参观\r\n3.每天的预订门票数上限数为300张\r\n4.开放时间为每天上午9:00—12:00时"
//    delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    [alertview show];
    //跳转到预定须知页面
    NSMutableArray *resultList = [NSMutableArray array];
    for (int i = 0, count = self.ticketsList.count; i < count; i ++) {
        
        NSDictionary *dic = [self.ticketsList objectAtIndex:i];
        [resultList addObject:dic];
        
    }
    BookingNoticeViewController *orderVC = [[BookingNoticeViewController alloc] initWithNibName:@"BookingNoticeViewController" bundle:nil];
    orderVC.ticketsList = resultList;
    [self.navigationController pushViewController:orderVC animated:YES];
    //
 
    
//    WetLandBookingViewController *bookingVC = [[WetLandBookingViewController alloc] initWithNibName:@"WetLandBookingViewController" bundle:nil];
//    [self.navigationController pushViewController:bookingVC animated:YES];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
    //跳转到注册
    RegisterViewController *registerVC = [[RegisterViewController alloc]initWithNibName:@"RegisterViewController" bundle:nil];
    [self.navigationController pushViewController:registerVC animated:YES];
    }
//    if (buttonIndex==1) {
//        if(showNotice == 1){
//            showNotice = 0;
//            // 循环保存票
//            NSMutableArray *resultList = [NSMutableArray array];
//            for (int i = 0, count = self.ticketsList.count; i < count; i ++) {
//                
//                NSDictionary *dic = [self.ticketsList objectAtIndex:i];
//                [resultList addObject:dic];
//                
//            }
//            //跳转到预定页  ,预定按扭用的
////            WetLandBookingOrderViewController *orderVC = [[WetLandBookingOrderViewController alloc] initWithNibName:@"WetLandBookingOrderViewController" bundle:nil];
////            orderVC.ticketsList = resultList;
////            [self.navigationController pushViewController:orderVC animated:YES];
//            
//            //BookingNoticeViewController 跳转到预定须知
//            BookingNoticeViewController *orderVC = [[BookingNoticeViewController alloc] initWithNibName:@"BookingNoticeViewController" bundle:nil];
//            orderVC.ticketsList = resultList;
//            [self.navigationController pushViewController:orderVC animated:YES];
//        }else{
//            showNotice = 0;
//            // 未登录 跳转到注册页
//        RegisterViewController *registerVC = [[RegisterViewController alloc]initWithNibName:@"RegisterViewController" bundle:nil];
//        [self.navigationController pushViewController:registerVC animated:YES];
//        }
//    }
}
@end
